<?php
	
	function he2str($he){
		$tmpHe = $he;
		$output = "";
		for($i=0; $i<strlen($tmpHe); $i=$i+1){
			if($i < strlen($tmpHe)-6){
				$tmpCh = substr($tmpHe, $i, 2);
			}else{
				$tmpCh="";
			}
			if($tmpCh == "%u"){
				$output = $output."&#x".substr($tmpHe, $i+2, 4).";";
				$i=$i+5;
			}else{
				$output = $output.substr($tmpHe, $i, 1);
			}
		}
		return $output;
	}

	function html_entity_decode_utf8($string) { 
	    static $trans_tbl; 
	 
	    // replace numeric entities 
	    $string = preg_replace('~&#x([0-9a-f]+);~ei', 'code2utf(hexdec("\\1"))', $string); 
	    $string = preg_replace('~&#([0-9]+);~e', 'code2utf(\\1)', $string); 
	 
    	// replace literal entities 
    	if (!isset($trans_tbl)){ 
        $trans_tbl = array(); 
 
        foreach (get_html_translation_table(HTML_ENTITIES) as $val=>$key) 
            $trans_tbl[$key] = utf8_encode($val); 
    	} 
 
  	 	return strtr($string, $trans_tbl); 
	}
	 
	function code2utf($num){ 
	    if ($num < 128) return chr($num); 
    	if ($num < 2048) return chr(($num >> 6) + 192) . chr(($num & 63) + 128); 
    	if ($num < 65536) return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); 
    	if ($num < 2097152) return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); 
    	return ''; 
	} 


	function uniord($ch) { 
    	$n = ord($ch{0}); 
	    if ($n < 128) { 
   	    	return $n; // no conversion required 
    	} 

    	if ($n < 192 || $n > 253) { 
        	return false; // bad first byte || out of range 
    	} 

    	$arr = array(1 => 192, // byte position => range from 
                 2 => 224, 
                 3 => 240, 
                 4 => 248, 
                 5 => 252, 
                 ); 

	    foreach ($arr as $key => $val) { 
    	    if ($n >= $val) { // add byte to the 'char' array 
        	    $char[] = ord($ch{$key}) - 128; 
        	    $range  = $val; 
        	} else { 
            	break; // save some e-trees 
        	} 
    	} 

	    $retval = ($n - $range) * pow(64, sizeof($char)); 

    	foreach ($char as $key => $val) { 
        	$pow = sizeof($char) - ($key + 1); // invert key 
        	$retval += $val * pow(64, $pow);   // dark magic 
    	} 

    	return $retval; 
	} 

	function fillZero($str){
		if(strlen($str) < 1){
			return "0000";
		}else if(strlen($str) < 2){
			return "000".$str;
		}else if(strlen($str) < 3){
			return "00".$str;
		}else if(strlen($str) < 4){
			return "0".$str;
		}else{
			return $str;
		}
	}

	function getUTF8($str){
		$output = "";
		$encStr = $str;
		for($i=0; $i<strlen($str); $i=$i+1){
			$tmpCh = uniord($encStr);
				if($tmpCh){
					if($tmpCh > 254){
						$encStr = substr($encStr, 3, strlen($encStr)-3);
						$i = $i + 2;
					}else{
						$encStr = substr($encStr, 1, strlen($encStr)-1);
 					}
					$tmpCh = strtoupper(dechex($tmpCh));
					$tmpCh = fillZero($tmpCh);
					$output = $output."&#x".$tmpCh.";";
				}else{ //Unknown charaters
					$output = $output.substr($encStr, 0, 1);
					$encStr = substr($str, 1, strlen($encStr)-1);
				}
		}
		return $output;
	}

	//echo he2str("%u524D%u5F71123456");
	//echo "<br>";
	//echo html_entity_decode_utf8(he2str("%u524D%u5F71123456"));
	//echo "<br>";
	//echo getUTF8('前影星01 APR 2010因為身體功能衰竭情報を掲載する際には、あらゆる面から細心');
	//echo "<br>";
	//echo urlencode(getUTF8('前影星01 APR 2010因為身體功能衰竭情報を掲載する際には、あらゆる面から細心'));
	//echo "<br>";
	
	
	function sendsms($telephone,$passcode){

			$username = "65729104";
			$password = "iho5wp89";
			$message = "Your verification code is ".$passcode.". Please fillin the verification code within the next 30 minutes.";
			$URL = "http://openapi.mdtechcorp.com:20000/openapi/?username=".$username."&password=".$password."&originatingAddress=".$username."&destinatingAddress=852".$telephone."&sms=".urlencode(getUTF8($message))."&returnMode=1&type=1";
			
			//CURL
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL,$URL);
			$output = curl_exec($ch);
			curl_close($ch);
	}
	
?>